//
//  SZVisitorView.m
//  Weibo
//
//  Created by Zahi on 2017/7/23.
//  Copyright © 2017年 Zahi. All rights reserved.
//

#import "SZVisitorView.h"

@interface SZVisitorView ()

/**环形图片**/
@property (nonatomic, strong) UIImageView *loopImgView;
/**遮罩图片**/
@property (nonatomic, strong) UIImageView *maskImgView;
/**环形图片**/
@property (nonatomic, strong) UIImageView *iconImgView;
/**提示标签**/
@property (nonatomic, strong) UILabel *messageLabel;
/**注册按钮**/
@property (nonatomic, strong) UIButton *registerBtn;
/**登录按钮**/
@property (nonatomic, strong) UIButton *loginBtn;



@end

@implementation SZVisitorView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}


#pragma mark - 设置UI
- (void)setupUI {
    self.backgroundColor = [UIColor colorWithWhite:237/255.0 alpha:1.0];
    
    [self addSubview:self.loopImgView];
    [self addSubview:self.maskImgView];
    [self addSubview:self.iconImgView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerBtn];
    [self addSubview:self.loginBtn];
    
    
    
    // 布局
    [self.loopImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [self.maskImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.loopImgView);
    }];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.maskImgView);
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.loopImgView.mas_bottom);
        make.width.mas_equalTo(224);
    }];
    
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.messageLabel);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(88, 35));
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.messageLabel);
        make.size.equalTo(self.registerBtn);
        make.centerY.equalTo(self.registerBtn);
    }];
    
}


#pragma mark - 动画

- (void)startAnimation
{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    anim.toValue = @(2* M_PI);
    
    anim.duration = 20;
    
    anim.repeatCount = MAXFLOAT;
    
    anim.removedOnCompletion = NO;
    
    [self.loopImgView.layer addAnimation:anim forKey:nil];
}



- (void)setupVisitorInfo:(NSString *)msg imageName:(NSString *)name isHome:(BOOL)isHome
{
    if (isHome) { // 首页
        [self startAnimation];
    } else { // 非首页
        self.loopImgView.hidden = YES;
        self.messageLabel.text = msg;
        self.iconImgView.image = [UIImage imageNamed:name];
    }
}


#pragma mark - 环形图片
- (UIImageView *)loopImgView
{
    if (_loopImgView == nil) {
        _loopImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
        
    }
    return _loopImgView;
}

#pragma mark 遮罩图片
- (UIImageView *)maskImgView
{
    if (_maskImgView == nil) {
        _maskImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    return _maskImgView;
}

#pragma mark 图标图片
- (UIImageView *)iconImgView
{
    if (_iconImgView == nil) {
        _iconImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    }
    return _iconImgView;
}

#pragma mark 提示标签
- (UILabel *)messageLabel
{
    if (_messageLabel == nil) {
        _messageLabel = [UILabel new];
        _messageLabel.text = @"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜";
        _messageLabel.font = [UIFont systemFontOfSize:13];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.numberOfLines = 0;
    }
    return _messageLabel;
}

#pragma mark 注册按钮
- (UIButton *)registerBtn
{
    if (_registerBtn == nil) {
        _registerBtn = [self buttonWithTitle:@"注册" action:@selector(registerButtonAction)];
    }
    return _registerBtn;
}


#pragma mark  登录按钮
- (UIButton *)loginBtn
{
    if (_loginBtn == nil) {
        _loginBtn = [self buttonWithTitle:@"登录" action:@selector(loginButtonAction)];
    }
    return _loginBtn;
}


#pragma mark - 设置按钮
- (UIButton *)buttonWithTitle:(NSString *)title action:(SEL)action
{
    UIButton *btn = [UIButton new];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:title forState:UIControlStateNormal];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    //  设置不同状态下文字的颜色
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    //  图片拉伸拉伸方式1
    
    UIImage *img = [[UIImage imageNamed:@"common_button_white_disable"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    
    //  设置背景图片
    [btn setBackgroundImage:img forState:UIControlStateNormal];
    //  取消点击高亮
    
    btn.adjustsImageWhenHighlighted = NO;
    
    return btn;
}

#pragma mark - 监听按钮的点击

- (void)registerButtonAction
{
    
    if (_visitorViewBlock) {
        _visitorViewBlock();
    }
    NSLog(@"注册");
}

- (void)loginButtonAction {
    if (_visitorViewBlock) {
        _visitorViewBlock();
    }
    NSLog(@"登录");
}



@end
